//
//  ChartDrawView.m
//  ChartDemoX
//
//  Created by yuanpt on 17/5/6.
//  Copyright © 2017年 yuanpt. All rights reserved.
//

#import "ChartDrawView.h"
#import "UIColor+Extensions.h"
#import "ChartUtils.h"
#import "ChartView.h"
#import "UIView+Extension.h"
#import "ChartLine.h"

@interface ChartDrawView()

@end

@implementation ChartDrawView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (!self) { return nil; }
    
    self.backgroundColor = [UIColor clearColor];
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    CGFloat widht = rect.size.width;
    CGFloat height = rect.size.height;
   
    CGPoint firstPoint = CGPointMake(0 - 2, 0);
    CGPoint secPoint = CGPointMake(widht, 0);
    CGPoint thirdPoint = CGPointMake(widht, height);
    CGPoint fourPoint = CGPointMake(0 - 2, height);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIColor *fourSideColor = [UIColor sy_chartLineColor];
    CGContextSetStrokeColorWithColor(context, fourSideColor.CGColor);
    CGContextSetLineWidth(context, 1.0f);
    
    [ChartUtils drawLine:context startPoint:firstPoint endPoint:secPoint];
    [ChartUtils drawLine:context startPoint:secPoint endPoint:thirdPoint];
    [ChartUtils drawLine:context startPoint:thirdPoint endPoint:fourPoint];
    [ChartUtils drawLine:context startPoint:fourPoint endPoint:firstPoint];
    
    CGFloat yMaxValue = self.chartView.yMaxValue;
    NSInteger yGridSplit = self.chartView.yGridSplit;
    CGFloat yGridValue = self.chartView.yGridValue;
    CGFloat yMinValue = self.chartView.yMinValue;
   
    // y
    CGFloat deltaValue = yMaxValue - yMinValue;
    // 有多少大格子
    CGFloat countOfSplit = deltaValue / yGridValue;
    
    // 每个大格子的高度
    CGFloat bigSplitHeigth = height / countOfSplit;
    
    
    for (NSInteger i = 1; i < (NSInteger)countOfSplit; i++) {
        CGPoint startP = CGPointMake(0 - 2, firstPoint.y + i * bigSplitHeigth);
        CGPoint endP = CGPointMake(widht, firstPoint.y + i * bigSplitHeigth);
        [ChartUtils drawLine:context startPoint:startP endPoint:endP];
    }
   
    NSInteger xGridSplit = self.chartView.xGridSplit;
    CGFloat xGridValue = self.chartView.xGridValue;
    CGFloat xMaxValue = self.chartView.xMaxValue;
    
    NSInteger xCount = (NSInteger)(widht / xGridValue);
    // x 大
    CGFloat bigSplitWidth = widht / xMaxValue;
    
    
    for (NSInteger i = 1; i < xCount; ++i) {
        CGPoint startP = CGPointMake(i * bigSplitWidth, 0);
        CGPoint endP = CGPointMake(i * bigSplitWidth, height);
        [ChartUtils drawLine:context startPoint:startP endPoint:endP];
    }
    
//     每个小格子的高度
    CGFloat smallSplitHeigth = bigSplitHeigth / yGridSplit;
    
   
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.9].CGColor);
    
    for (NSInteger i = 1; i < (NSInteger)(height / smallSplitHeigth); ++i) {
        CGPoint startP = CGPointMake(0 - 2, firstPoint.y + i * smallSplitHeigth);
        CGPoint endP = CGPointMake(widht, firstPoint.y + i * smallSplitHeigth);
        [ChartUtils drawLine:context startPoint:startP endPoint:endP];
    }
   
    // x 小
    CGFloat smallHeight = bigSplitWidth / xGridSplit;
    for (NSInteger i = 1; i < (NSInteger)(widht / smallHeight); ++i) {
        CGPoint startP = CGPointMake(i * smallHeight, 0);
        CGPoint endP = CGPointMake(i * smallHeight, height);
        [ChartUtils drawLine:context startPoint:startP endPoint:endP];
    }
    

    UIColor *color;
    color = [UIColor sy_colorWithHexString:@"#E2F0DD" alpha:0.7];
    [color set];
    UIBezierPath *closedPath = self.chartView.closedPath;
    closedPath.lineWidth = 2.0f;
    [self.chartView.closedPath fill];
    [self.chartView.closedPath stroke];
    color = [UIColor sy_colorWithHexString:@"#E3E6E3" alpha:0.9];
    [color set];
    UIBezierPath *nor = self.chartView.normalLine.path;
    nor.lineWidth = 1.0f;
    
    [nor stroke];
    
    [[self.chartView.maxLine path] stroke];
    
    [[self.chartView.minLine path] stroke];
    
    // 今天
    CGFloat radius = 3;
    CGRect todayRect = CGRectMake(self.chartView.xToday - radius, 0, 60, 30);
    [[UIColor grayColor] set];
    UIBezierPath *todayBP = [UIBezierPath bezierPathWithRoundedRect:todayRect cornerRadius:radius];
    [todayBP fill];
    NSString *todayString = @"今天";
    NSDictionary *toDayattr = @{NSFontAttributeName: [UIFont systemFontOfSize:11.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
    CGSize todaySize = [todayString sizeWithAttributes:toDayattr];
    [todayString drawAtPoint:CGPointMake(todayRect.origin.x  + 0.5 * todayRect.size.width - todaySize.width * 0.5, todayRect.origin.y  + 0.5 * todayRect.size.height - todaySize.height * 0.5) withAttributes:toDayattr];
    
    [[UIColor grayColor] set];
    [ChartUtils drawLine:context startPoint:CGPointMake(self.chartView.xToday, 0) endPoint:CGPointMake(self.chartView.xToday, height)];
    
    ChartLine *recLine = [self.chartView recordLine];
    color = [UIColor redColor];
    [color set];
    [recLine.path stroke];

    [[UIColor redColor] set];
    [recLine.points enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGPoint p = [obj CGPointValue];

        CGRect frame = CGRectMake(0, 0, 8, 8);
        frame.origin.x = p.x - 4;
        frame.origin.y = p.y - 4;
        
        CGContextAddEllipseInRect(context, frame);

        CGContextFillPath(context);
    }];
}

@end
